﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;


namespace DataProcessor.Process
{
    class Util
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Util));

        private static List<污染物编码> _contaminants;
        private static List<污染物对照> _contaminantReferences;
        public static 污染物编码 GetContaminantByCode(string code)
        {
            try
            {
                if (_contaminants == null | _contaminantReferences == null ||
                    MyTimer.IsToTheTime("Util.GetContaminantByCode", 10))
                {
                    using (var ctx = new PlatformXpfContext())
                    {
                        _contaminants = ctx.污染物编码.AsNoTracking().ToList();
                        _contaminantReferences = ctx.污染物对照.AsNoTracking().ToList();
                    }
                }
                var reference = _contaminantReferences.FirstOrDefault(t => t.污染物新代码.Equals(code));
                if (reference != null)
                {
                    code = reference.污染物代码;
                }
                if (code != null)
                {
                    return _contaminants.FirstOrDefault(t => t.污染物代码.Equals(code));
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;

        }
        
        private static List<企业排口> _enterpriseInfoBuffer;
        /// <summary>
        /// 根据Mn获取排口信息
        /// </summary>
        /// <param name="mn"></param>
        /// <returns></returns>
        public static 企业排口 GetInfoFromMn(string mn)
        {
            try
            {
                if (_enterpriseInfoBuffer == null || MyTimer.IsToTheTime("Util.GetInfoFromMn", 10))
                {
                    using (var ctx = new PlatformXpfContext())
                    {
                        _enterpriseInfoBuffer = ctx.企业排口.AsNoTracking().ToList();
                    }
                }
                return _enterpriseInfoBuffer.FirstOrDefault(t => t.MN号.Equals(mn));

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;
        }

        /// <summary>
        /// 查找串中主污染物代码（根据配置文件中主污染物代码）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetMasterPollutionCode(List<string> source)
        {
            string masterString = Properties.Settings.Default.MasterPollutants;
            var master = masterString.Split(';');
            return master.FirstOrDefault(item => source.Contains(item));
        }

        public static DateTime OffsetMinute(DateTime time, int minute)
        {
            return new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0).AddMinutes(minute);
        }


        public static DateTime TrimToHour(DateTime time, int hours)
        {
            return new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0).AddHours(hours);
        }
        public static DateTime TrimToDay(DateTime time, int days)
        {
            return new DateTime(time.Year, time.Month, time.Day).AddDays(days);
        }
        public static DateTime TrimToMonth(DateTime time, int months)
        {
            return new DateTime(time.Year, time.Month, 1).AddMonths(months);
        }
        public static DateTime TrimToYear(DateTime time, int year)
        {
            return new DateTime(time.Year, 1, 1).AddYears(year);
        }

        private static string mns = "756877X0243939;756877X0243950;756877X0243493";
        private static Random random = new Random();
        public static bool DontProcess(string mn)
        {
            if (!mns.Contains(mn)) return false;
            if (random.Next(100) < 40) return true;
            return false;
        }
    }
}
